<?php

namespace app\meizhuangapi\controller\sharp;

use app\api\controller\sharp\Goods as GoodsModel;
use app\api\service\sharp\Active as ActiveService;
use think\Db;
use app\common\service\qrcode\sharp\Goods as GoodsPoster;
use app\meizhuangapi\model\meizhuang\MeizhuangComment as MeizhuangCommentModel;
/**
 * 整点秒杀-商品管理
 * Class Goods
 * @package app\api\controller\sharp
 */
class Goods extends GoodsModel
{
    /**
     * 秒杀活动商品列表(弃用)
     */
    public function lists(){
        // 获取秒杀活动会场首页数据
        $active_time_id=input('post.active_time_id');
        $service = new ActiveService;
        $list = $service->getGoodsListByActiveTimeId($active_time_id);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 获取活动商品详情
     */
    public function detail(){
        $active_time_id=input('post.active_time_id');
        $sharp_goods_id=input('post.sharp_goods_id');
        // 获取秒杀活动商品详情
        $service = new ActiveService;
        $user= $this->getUser(false);
        $sharpInfo=Db::name('sharp_goods')->where(['sharp_goods_id'=>$sharp_goods_id])->find();
        $data = $service->getyActiveGoodsDetail($active_time_id, $sharp_goods_id);
        $data['canshuData']=$this->getGoodsCanshu($sharpInfo['goods_id']);
        $collect=Db::name('meizhuang_collect')->where(['goods_id'=>$sharpInfo['goods_id'],'user_id'=>$user['user_id']])->find();
        if($collect){
            $collectStatus=1;
        }else{
            $collectStatus=0;
        }
        $pingjiaData=$this->getPingJiaList($sharpInfo['goods_id']);
        $data['pingjiaData']=$pingjiaData;
        $data['collectStatus']=$collectStatus;
        $data['goods']['canshu']=strtr($data['goods']['canshu'], '#', ' ');
        $data['goods']['canshu']=strtr($data['goods']['canshu'], '*', ':');
        if ($data === false) {
            return $this->renderError($service->getError());
        }
        return $this->renderSuccess($data);
    }
    /*
   * 获取商品评价列表
   */
    private function getPingJiaList($goods_id){
        $where['goods_id']=$goods_id;
        $where['status']=1;
        $commentList=(new MeizhuangCommentModel())->getList($where);
        if($commentList['data']){
            foreach ($commentList['data'] as $k=>$v){
                if(!empty($v['user']['username'])){
                    $commentList['data'][$k]['user']['nickName']=$v['user']['username'];
                }
            }
        }
        $return['commentList']=$commentList;
        return $return;
    }
    /*
     * 获取产品参数
     */
    private function getGoodsCanshu($goods_id){
        $secInfo=Db::name('goods')->where(['goods_id'=>$goods_id])->field('canshu')->find();
        $arr=[];
        if($secInfo){
            $a1=explode('#',$secInfo['canshu']);
            foreach ($a1 as $k=>$v){
                $a2=explode('*',$v);
                $arr[$k]['key']=$a2[0];
                $arr[$k]['value']=$a2[1];
            }
        }
        return $arr;
    }

    /**
     * 生成商品海报
     */
    public function poster(){
        // 获取秒杀活动商品详情
        $active_time_id=input('post.active_time_id');
        $sharp_goods_id=input('post.sharp_goods_id');
        $service = new ActiveService;
        $data = $service->getyActiveGoodsDetail($active_time_id, $sharp_goods_id);
        if ($data === false) {
            return $this->renderError($service->getError());
        }
        // 生成商品海报图
        $Qrcode = new GoodsPoster($data['active'], $data['goods'], $this->getUser(false));
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getImage(),
        ]);
    }
}